﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

using Bricks.Component.Utils;
using Bricks.Business.Models;
using Bricks.Business.Models.ProjectContract;
using Bricks.Business.Models.Account;
using Bricks.Site.Models;
using Bricks.Business.Data.Repositories.AlterManagement;
using Bricks.Business.Data.Repositories.ProjectContract;
using Bricks.Business.Service;
using Bricks.Business.Data.Repositories.Account;


namespace ChiDao.Business.Service.Impl
{
    [Export(typeof(ICommonContractInfoService))]
    internal class CommonContractInfoService : ICommonContractInfoService
    {
        [Import]
        protected ICommonContractInfoRepository CommonContractInfoRepository { get; set; }
        [Import]
        protected IOrgInfoRepository OrgInfoRepository { get; set; }

        public IQueryable<CommonContractInfo> CommonContractInfos
        {
            get { return CommonContractInfoRepository.Entities; }
        }

        public ContractInfoView GetCommonContractInfo(Guid id)
        {
            ContractInfoView contractInfoView = null;
            CommonContractInfo record = CommonContractInfos.FirstOrDefault(s => s.ID == id);
            if (record != null)
            {
                contractInfoView = new ContractInfoView();
                contractInfoView.ID = record.ID;
                contractInfoView.PID = record.PID;
                contractInfoView.ContractUnitName = record.ContractUnitName;
                contractInfoView.ContractCode = record.ContractCode;
                contractInfoView.ContractName = record.ContractName;
                contractInfoView.ContractType = record.ContractType;
                contractInfoView.ContractMoney = record.ContractMoney;
                contractInfoView.ScheduledStartDate = record.ScheduledStartDate.HasValue ? record.ScheduledStartDate.Value.ToString("yyyy-MM-dd") : string.Empty;
                contractInfoView.ScheduledEndDate = record.ScheduledEndDate.HasValue ? record.ScheduledEndDate.Value.ToString("yyyy-MM-dd") : string.Empty; ;
                contractInfoView.ProjectPeriod = record.ProjectPeriod;
                contractInfoView.CorrespondenceUnit = record.CorrespondenceUnit;
                contractInfoView.CorrespondenceUnitName = GetOrgInfoByID(record.CorrespondenceUnit);
                contractInfoView.ContractContent = record.ContractContent;
                contractInfoView.Attachment = record.Attachment;
            }
            return contractInfoView;
        }

        public string GetOrgInfoByID(Guid id)
        {
            string result = string.Empty;
            var org = OrgInfoRepository.Entities.FirstOrDefault(m => m.ID == id);
            if (org != null)
            {
                result = org.OrgName;
            }
            return result;
        }

        public string GetCommonContractName(Guid id)
        {
            CommonContractInfo record = CommonContractInfos.FirstOrDefault(s => s.ID == id);
            string contractName = string.Empty;
            if (record != null)
            {
                contractName = record.ContractName;
            }
            return contractName;
        }

        public OperationResult SaveCommonContractInfo(ContractInfoView contractInfoView)
        {
            try
            {
                CommonContractInfo record = CommonContractInfos.FirstOrDefault(s => s.ID == contractInfoView.ID);
                if (record != null)
                {
                    Modify(record, contractInfoView);
                }
                else
                {
                    Add(contractInfoView);
                }
                return new OperationResult(OperationResultType.Success, "操作成功");
            }
            catch (Exception ex)
            {
                return new OperationResult(OperationResultType.Error, ex.Message);
            }
        }

        private void Add(ContractInfoView contractInfoView)
        {
            CommonContractInfo record = new CommonContractInfo();
            record.ID = contractInfoView.ID;
            record.PID = contractInfoView.PID;
            record.ContractUnitName = contractInfoView.ContractUnitName;
            record.ContractCode = contractInfoView.ContractCode;
            record.ContractName = contractInfoView.ContractName;
            record.ContractMoney = contractInfoView.ContractMoney;
            if (!String.IsNullOrEmpty(contractInfoView.ScheduledStartDate))
                record.ScheduledStartDate = Convert.ToDateTime(contractInfoView.ScheduledStartDate);
            else
                record.ScheduledStartDate = null;
            if (!String.IsNullOrEmpty(contractInfoView.ScheduledEndDate))
                record.ScheduledEndDate = Convert.ToDateTime(contractInfoView.ScheduledEndDate);
            else
                record.ScheduledEndDate = null;
            record.ProjectPeriod = contractInfoView.ProjectPeriod;
            record.CorrespondenceUnit = contractInfoView.CorrespondenceUnit;
            record.ContractContent = contractInfoView.ContractContent;
            record.Attachment = contractInfoView.Attachment;
            CommonContractInfoRepository.Insert(record);
        }

        private void Modify(CommonContractInfo record, ContractInfoView contractInfoView)
        {
            record.ContractUnitName = contractInfoView.ContractUnitName;
            record.ContractCode = contractInfoView.ContractCode;
            record.ContractName = contractInfoView.ContractName;
            record.ContractMoney = contractInfoView.ContractMoney;
            if (!String.IsNullOrEmpty(contractInfoView.ScheduledStartDate))
                record.ScheduledStartDate = Convert.ToDateTime(contractInfoView.ScheduledStartDate);
            else
                record.ScheduledStartDate = null;
            if (!String.IsNullOrEmpty(contractInfoView.ScheduledEndDate))
                record.ScheduledEndDate = Convert.ToDateTime(contractInfoView.ScheduledEndDate);
            else
                record.ScheduledEndDate = null;
            record.ProjectPeriod = contractInfoView.ProjectPeriod;
            record.CorrespondenceUnit = contractInfoView.CorrespondenceUnit;
            record.ContractContent = contractInfoView.ContractContent;
            record.Attachment = contractInfoView.Attachment;
            CommonContractInfoRepository.Insert(record);
        }

        public OperationResult Delete(Guid id)
        {
            PublicHelper.CheckArgument(id, "id");
            CommonContractInfo record = CommonContractInfos.FirstOrDefault(s => s.ID == id);
            if (record == null)
                return new OperationResult(OperationResultType.QueryNull, "未找到指定的记录");

            if (CommonContractInfoRepository.Delete(record) >= 0)
                return new OperationResult(OperationResultType.Success, "删除成功", record);
            return new OperationResult(OperationResultType.Error, "删除失败");
        }
    }
}